package com.bizframework.core.testcase.interagtion.dao;

import java.io.Serializable;

import org.junit.Test;

import com.bizframework.core.testcase.interagtion.spring.SpringTxTestCase;

import com.bizframework.core.domain.BaseEntity;
import com.bizframework.core.domain.BaseDao;

public abstract class BaseEntityDaoTest<T extends BaseEntity<IDT>, IDT extends Serializable> extends SpringTxTestCase {

	@Test
	public void testCRUDEntity() throws Exception {
		// new entity and save it.
		T entity = createEntity();
		getDao().save(entity);
		getDao().flush();
		
		// find entity.
		T entityFromDB = getDao().getById(entity.getId());
		assertReflectionEquals(entity, entityFromDB);
		getDao().flush();
		getDao().refresh(entityFromDB);
		
		//modify entity
		modifyEntity(entity);
		getDao().update(entity);
		getDao().flush();
		getDao().refresh(entityFromDB);
		entityFromDB = getDao().getById(entity.getId());
		assertReflectionEquals(entity, entityFromDB);
		
		// delete entity.
		getDao().delete(entity);
		getDao().flush();
		entity = getDao().getById(entity.getId());
		assertNull(entity);
	}
	
	protected abstract T createEntity() throws Exception;
	
	protected void modifyEntity(T entity) throws Exception{
	}
	
	protected abstract BaseDao<T, IDT> getDao();
}
